import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.12

//qmlRegisterType注册
import GAME 1.0


Window {
    id: rootWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("SNAKE")
    flags: Qt.FramelessWindowHint
    property bool isRun: false
    property bool isNormal: true

    Rectangle{
        id: menu
        width: parent.width
        height: 40
        border {
            width: 1
            color: "transparent"
        }

        /* MouseArea放在前面，避免覆下面控件功能 */
        MouseArea {
            anchors.fill: parent
            property point clickPos: "0,0"
            onPressed: {
                clickPos  = Qt.point(mouseX, mouseY)
            }
            onPositionChanged: {
                var delta = Qt.point(mouseX-clickPos.x, mouseY-clickPos.y)
                rootWindow.setX(rootWindow.x+delta.x)
                rootWindow.setY(rootWindow.y+delta.y)
            }
        }

        LinearGradient {
                anchors.fill: parent
                start: Qt.point(0, 0)
                end: Qt.point(parent.width, 0)
                gradient: Gradient {
                    GradientStop {position: 0.0; color: "#a1c4fd"}
                    GradientStop {position: 1.0; color: "#c2e9fb"}
                }
            }

        Text {
            id: levelStr
            text: qsTr("难度")
            width: 50
            height: parent.height - 10
            anchors {
                top: parent.top
                left: parent.left
                topMargin: 5
            }
            color: 'blue'
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }

        ComboBox {
            id: level
            width: 100
            height: parent.height - 10
            anchors{
                top: parent.top
                left: levelStr.right
                topMargin: 5
            }
            currentIndex: 0
            model: [1, 2, 3, 4]
            background: Rectangle {
                border.width: 1
                radius: 2
            }
        }

        Text {
            id: gridStr
            text: qsTr("格数")
            width: 50
            height: parent.height - 10
            anchors {
                leftMargin: 30
                left: level.right
                top: parent.top
                topMargin: 5
            }
            color: 'blue'
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }

        ComboBox {
            id: sgrid
            width: 100
            height: parent.height - 10
            anchors{
                top: parent.top
                left: gridStr.right
                topMargin: 5
            }
            currentIndex: 0
            model: ["100x100", "80x80", "60x60", "40x40"]
            background: Rectangle {
                border.width: 1
                radius: 2
            }
        }

        Button {
            id: btnClose
            width: 40
            text: qsTr("X")
            height: 40
            anchors{
                right: parent.right
            }
            font{
                pixelSize: 16
                family: "Microsoft YaHei"
                bold: true
            }
            contentItem: Text {
                    text: parent.text
                    font: parent.font
                    opacity: enabled ? 1.0 : 0.3
                    color: "#FFFFFF"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
            }
            background: Rectangle{
                color: "transparent"
            }
            onClicked: close()

        }

        Button {
            width: 40
            text: qsTr("\u53e3")
            height: 40
            anchors{
                right: btnClose.left
                rightMargin: 0
            }
            font{
                pixelSize: 16
                family: "Microsoft YaHei"
                bold: true
            }
            contentItem: Text {
                    text: parent.text
                    font: parent.font
                    opacity: enabled ? 1.0 : 0.3
                    color: "#FFFFFF"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
            }
            background: Rectangle{
                color: "transparent"
            }
            onClicked: {
                if(isNormal) showMaximized();
                else showNormal();
                isNormal = !isNormal
            }
        }
    }

    SNAKE{
        id: snake
        focus: true;
        width: parent.width
        height: parent.height - 20
        anchors.top: menu.bottom

        MouseArea {
            anchors.fill: parent
            onClicked: {
                forceActiveFocus()
                if(isRun) {snake.pause()}
                else {snake.start()}
                isRun = !isRun
            }
            onDoubleClicked: snake.restart(sgrid.currentIndex, level.currentIndex)
        }

        Keys.onSpacePressed:{
            if(isRun) {pause()}
            else {start()}
            isRun = !isRun
        }
        Keys.onLeftPressed: setDirect(0)
        Keys.onUpPressed:   setDirect(1)
        Keys.onRightPressed:setDirect(2)
        Keys.onDownPressed: setDirect(3)
    }

}
